<?php

namespace App\Admin\Controllers\Audit;

use App\Admin\Actions\Audit\FeedbackAuditAction;
use App\Admin\Grid\NGrid;
use App\Models\ShopFeedbackModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;

class AuditFeedbackController extends AdminController
{
    protected $title = '意见反馈';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new ShopFeedbackModel);
        $grid->model()->leftJoin('shop_user', 'shop_user.user_id', 'shop_feedback.user_id')->select([
            'shop_feedback.*',
            'shop_user.user_mobile'
        ])->where('shop_feedback.status', 0);
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('fb_id', 'ID');
                $filter->equal('fb_type', __('auction.fbType'))->select(__('feedback.fbTypeArr'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('serial_number', __('l.serialNumber'));
            });
        });
        $grid->column('fb_id', 'ID')->sortable();
        $grid->column('serial_number', __('l.serialNumber'));
        $grid->column('user_mobile', __('l.user_mobile'));
        $grid->column('fb_type', __('feedback.fbType'))->using(__('feedback.fbTypeArr'));
        $grid->column('fb_source_id', __('feedback.fbSourceId'));
        $grid->column('fb_content', __('feedback.fbContent'));
        $grid->column('fb_mobile', __('feedback.fbMobile'));
        $grid->column('file_ids', __('feedback.fileIds'))->display(function ($column) {
            if (!empty($column)) return view('file_ids', ['id' => 'certification-' . $this->id, 'images' => $column]);
            else return null;
        });
        $grid->column('created_at', __('l.createdAt'))->display(function ($column) {
            return now()->timestamp($column)->toDateTimeString();
        });
        $grid->actions(function (Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
            if ($actions->row->status === 0) $actions->add(new FeedbackAuditAction);
        });
        $grid->disableActions(false);
        return $grid;
    }
}
